<?php
/**
 * User: Administrator
 * Email: aphie1900@sina.com
 * Date & Time: 2017/9/15 0015 14:17
 */

namespace app\api\controller\v1;


use app\api\validate\Count;
use app\api\model\Product as ProductModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\ProductException;

class Product
{
    public function getRecent($count = 15)
    {
        (new Count())->goCheck();
        $recentProducts = ProductModel::getRecentProducts($count);
        if ($recentProducts->isEmpty()) {
            throw new ProductException();
        }
        $recentProducts->hidden(['summary']);
        return $recentProducts;
    }

    public function getProductsByCategoryId($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductsByCategoryId($id);
        if ($products->isEmpty()) {
            throw new ProductException();
        }
        $products->hidden(['summary']);
        return $products;
    }
}